package recallin.activities;

import org.mapsforge.core.GeoPoint;

import recallin.database.DatabaseUtility;
import recallin.gui.NavigationAdapter;
import recallin.gui.animation.CloseAnim;
import recallin.gui.animation.ShowAnim;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author philipmasek
 * Menu navigation. Handles SQLite connection and search for addresses.
 */


public class NavigationActivity extends Activity {

	private boolean topOpen = false;
	private Handler mHandler;
	private NavigationAdapter adapter1;
	private NavigationAdapter adapter2;
	private Button start;
	private static TextView pedestrian;
	private static TextView car;
	private static GeoPoint startPoint;
	private static GeoPoint endPoint;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.navi_menu);

		start = (Button) findViewById(R.id.start_navigation);

		pedestrian = (TextView) findViewById(R.id.navi_pedestrian);
		car = (TextView) findViewById(R.id.navi_car);
//		setMode(CAR_MODE);
//
//		pedestrian.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				NavigationActivity.this.setMode(PEDESTRIAN_MODE);
//			}
//		});
//		car.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				NavigationActivity.this.setMode(CAR_MODE);
//			}
//		});

		final EditText searchField1 = (EditText) findViewById(R.id.navi_searchfield1);
		final EditText searchField2 = (EditText) findViewById(R.id.navi_searchfield2);

		final ListView listView1 = (ListView) findViewById(R.id.navi_listview1);
		final ListView listView2 = (ListView) findViewById(R.id.navi_listview2);

		Thread thread = new Thread() {
			@Override
			public void run() {
				Looper.prepare();
				adapter1 = new NavigationAdapter(NavigationActivity.this,
						getStreets("A", 150));
				adapter2 = new NavigationAdapter(NavigationActivity.this,
						getStreets("A", 150));
				mHandler.post(new Runnable() {
					@Override
					public void run() {
						listView1.setAdapter(adapter1);
						listView2.setAdapter(adapter2);
					}
				});
				super.run();
			}
		};
		thread.start();

		MenuActivity.setupSearchField(this, searchField1);
		MenuActivity.setupSearchField(this, searchField2);

		listView1.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				listView1.requestFocus();
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
		listView2.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				listView2.requestFocus();
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});

		listView1.setTextFilterEnabled(true);
		listView2.setTextFilterEnabled(true);

		listView1.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> av, View v, int pos, long p) {
				adapter1.getCursor().moveToPosition(pos);
//				System.out.println(adapter1.getCursor().getString(2) + "  :  "
//						+ adapter1.getCursor().getDouble(0) + ", "
//						+ adapter1.getCursor().getDouble(1));
				MainActivity.setStartPoint(new GeoPoint(adapter1.getCursor()
						.getDouble(0), adapter1.getCursor().getDouble(1)));
				searchField1.setText(adapter1.getCursor().getString(2) + ", "
						+ adapter1.getCursor().getString(3));
				closeList(listView1);
				setTopOpen(false);
			}
		});
		listView2.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> av, View v, int pos, long p) {
				adapter2.getCursor().moveToPosition(pos);
//				System.out.println(adapter2.getCursor().getString(2) + "  :  "
//						+ adapter2.getCursor().getDouble(0) + ", "
//						+ adapter2.getCursor().getDouble(1));

				MainActivity.setDestinationPoint(new GeoPoint(adapter2
						.getCursor().getDouble(0), adapter2.getCursor()
						.getDouble(1)));
//				System.out.println(MainActivity.getDestinationPoint()
//						.getLatitude());

				searchField2.setText(adapter2.getCursor().getString(2) + ", "
						+ adapter2.getCursor().getString(3));
			}
		});

		searchField1.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(final CharSequence s, int start,
					int count, int after) {
				if (s.length() > 0 && !isTopOpen()) {
					mHandler.post(new Runnable() {
						public void run() {
							openList(listView1);
						}
					});
					setTopOpen(true);
				} else if (s.length() == 0) {
					mHandler.post(new Runnable() {
						public void run() {
							closeList(listView1);
						}
					});
					setTopOpen(false);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() > 0)
					adapter1.getFilter().filter(s);

			}
		});

		mHandler = new Handler();

		start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if (Character.isDigit(searchField1.getText().charAt(0))) {
//					String[] tmp = searchField1.getText().toString().split(", ");
//					Double lat = Double.parseDouble(tmp[0]);
//					Double lon = Double.parseDouble(tmp[1]);
//					MainActivity.setStartPoint(new GeoPoint(lat, lon));
//					String[] tmp1 = searchField2.getText().toString().split(", ");
//					Double lat1 = Double.parseDouble(tmp[0]);
//					Double lon1 = Double.parseDouble(tmp[1]);
//					MainActivity.setDestinationPoint(new GeoPoint(lat1, lon1));
//					MainActivity.startingRoute = true;
//					finish();
//				}else
				if (MainActivity.getDestinationPoint() != null) {
					if (searchField1.getText() == null)
						MainActivity
								.setDestinationPoint(new GeoPoint(MainActivity
										.getCurrentLocation().getLatitude(),
										MainActivity.getCurrentLocation()
												.getLongitude()));

					MainActivity.startingRoute = true;
					finish();
				} else
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(NavigationActivity.this,
									"Select a end point", Toast.LENGTH_SHORT)
									.show();
						}
					});
			}
		});

		searchField2.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// adapter2.getFilter().filter(arg0);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				adapter2.getFilter().filter(s);
			}
		});

	}

	private void openList(ListView listView) {
		Animation show = new ShowAnim(listView, getWindowManager()
				.getDefaultDisplay().getHeight());
		show.setDuration(900);
		listView.startAnimation(show);
	}

	private void closeList(ListView listView) {
		Animation close = new CloseAnim(listView, getWindowManager()
				.getDefaultDisplay().getHeight());
		close.setDuration(900);
		listView.startAnimation(close);
	}

	public boolean isTopOpen() {
		return topOpen;
	}

	public void setTopOpen(boolean topOpen) {
		this.topOpen = topOpen;
	}

	public Cursor getStreets(CharSequence constraint, int size) {
		if (constraint == null || constraint.length() == 0) {
			// Return the full list of countries
			return DatabaseUtility.getDatabase()
					.rawQuery(
							"SELECT * FROM Streets ORDER BY Street LIMIT "
									+ size, null);
		} else {
			// Return a list of countries who's name contains the passed in
			// constraint
			return DatabaseUtility.getDatabase().rawQuery(
					"SELECT * FROM Streets WHERE Street LIKE '"
							+ constraint.toString()
							+ "%' ORDER BY Street LIMIT " + size, null);
		}
	}

	public static GeoPoint getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(GeoPoint startPoint) {
		this.startPoint = startPoint;
	}

	public static GeoPoint getEndPoint() {
		return endPoint;
	}

	public void setEndPoint(GeoPoint endPoint) {
		this.endPoint = endPoint;
	}



}
